Yacon.widget.TButton = function(element){
	this.element = element;
};

Yacon.widget.TButton.prototype = {
	
	getHtml: function(){
		
			
	},
	
	/* 获取顶部按钮HTML */
	getTopHtml: function(){
		
		var html = "";
		
		html += "<a btn-id='"+this.element.id+"' lily-player='button' class='btn-setting lily-button-top' ";
		
		if(this.element.click){
			html += " view-click='"+this.element.click+"' ";
		}
		
		if(this.control){
			html += " view-control='"+this.control+"' ";
		}
		
		html += this.getPopHtml();
		
		html += this.getTooltipHtml();
		
		html += " style='cursor:pointer;' >"
		
		html += "<span class='"+this.element.ico+" icon'></span></a>";
		
		return html;
		
	},
	
	/* 获取右键按钮HTML */
	getRightHtml: function(){
		
		var html = "";
		
		if(this.element.id == "divider"){
			html += "<li class='divider'></li>";
			return html;
		}
		
		html += "<li lily-player='button' btn-id='"+this.element.id+"' ";
		
		if(this.element.click){
			html += " view-click='"+this.element.click+"' ";
		}
		
		if(this.control){
			html += " view-control='"+this.control+"' ";
		}
		
		html += this.getPopHtml();
		
		html += this.getTooltipHtml();
		
		html += ">";
		
		html += "<a tabindex='-1'>";
		
		html += "<span class='"+this.element.ico+"'></span>";
		html += "&nbsp;&nbsp;&nbsp;"
		html += this.element.name;
		
		html += "</a>";		
		html += "</li>"
		
		return html;
		
	},
	
	/* 获取底部Html方法 */
	getBottomHtml: function(){
		
		var html = "";
		
		//普通按钮颜色修改
		html += "<button type='button' lily-player='button' btn-id='"+this.element.id+"'  ";
		
		var btnClass = "btn ";
		
		if(!Yacon.isEmpty(this.element.btnState)){
			btnClass += " btn-"+this.element.btnState;
		}else{
			btnClass += " lily-button-bottom";
		}
		
		html += "class='"+btnClass+"'";
		
		var btnColor = "color:#FFF !important;";
		
		if(!Yacon.isEmpty(this.element.fontColor)){
			btnColor = "color:"+this.element.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(this.element.backColor)){
			btnColor += " background-color:"+this.element.backColor+" !important;";
			btnColor += " border-color:"+this.element.backColor+" !important;";
		}
		
		html+= "style='"+btnColor+"'";
		
		if(this.element.click){
			html += " view-click='"+this.element.click+"' ";
		}
		
		if(this.control){
			html += " view-control='"+this.control+"' ";
		}
		
		html += this.getPopHtml();
		
		html += this.getTooltipHtml();
		
		html += ">";
		
		html += this.getLableHtml();
		
		html += "</button>";
		
		return html;
		
	},
	
	getFormHtml: function(){
		
		var html = "";
		
		html += "<button type='button' lily-player='button' btn-id='"+this.element.id+"' ";
		
		var btnClass = "btn btn-default "
			
		if(!Yacon.isEmpty(this.element.btnState)){
			btnClass += "lily-form-button-"+this.element.btnState;
		}else{
			btnClass += "lily-button-form";
		}
		
		html += " class='"+btnClass+"' ";
		
		if(this.element.click){
			html += " view-click='"+this.element.click+"' ";
		}
		
		if(this.control){
			html += " view-control='"+this.control+"' ";
		}
		
		var btnColor = "";
		
		if(!Yacon.isEmpty(this.element.fontColor)){
			btnColor += "color:"+this.element.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(this.element.backColor)){
			btnColor += " background-color:"+this.element.backColor+" !important;";
			btnColor += " border-color:"+this.element.backColor+" !important;";
		}
		
		if(!Yacon.isEmpty(btnColor)){
			html += " style='"+btnColor+"' ";
		}
		
		html += this.getPopHtml();
		
		html += this.getTooltipHtml();
		
		html += ">";
		
		html += this.getLableHtml();
		
		html += "</button>";
		
		return html;
		
	},
	
	getLableHtml: function(){
		
		var html = "";
		var lable = this.element.name;
		var lableIco = this.element.ico;
		var lableType = this.element.viewType;
		
		if(Yacon.isEmpty(lableIco)){
			lableIco = "glyphicon glyphicon-tag";
		}
		
		if(lableType == "all"){
			html += "<span class='"+lableIco+"'></span>&nbsp;&nbsp;"+lable;
		}
		else if(lableType == "ico"){
			html += "&nbsp;<span class='"+lableIco+"'></span>&nbsp;";
		}
		else if(lableType == "label"){
			html += lable;
		}
		
		return html;
	},
	
	getPopHtml:function(){
		
		var html = "";
		
		if(Yacon.isEmpty(this.element.popTitle) && Yacon.isEmpty(this.element.popContent)){
			return html;
		}
		
		if(!Yacon.isEmpty(this.element.popTitle)){
			html += " title='"+this.element.popTitle+"' "
		}
		
		if(!Yacon.isEmpty(this.element.popContent)){
			html += " data-content='"+this.element.popContent+"' "
		}

		if(!Yacon.isEmpty(this.element.popPlace)){
			html += " data-placement='"+this.element.popPlace+"' ";
		}
		
		if(!Yacon.isEmpty(this.element.popTigger)){
			html += " data-trigger='"+this.element.popTigger+"' ";
		}
		
		html += " data-html='true' ";
		
		html += " data-container='body' ";
		
		html += " data-toggle='popover' ";
		
		return html;
	},
	
	getTooltipHtml:function(){
		
		var html = "";
		
		if(Yacon.isEmpty(this.element.tooltipTitle)){
			return html;
		}
		
		if(!Yacon.isEmpty(this.element.tooltipTitle)){
			html += " title='"+this.element.tooltipTitle+"' "
		}

		if(!Yacon.isEmpty(this.element.tooltipPlace)){
			html += " data-placement='"+this.element.tooltipPlace+"' ";
		}
		
		if(!Yacon.isEmpty(this.element.tooltipTigger)){
			html += " data-trigger='"+this.element.tooltipTigger+"' ";
		}
		
		html += " data-container='body' ";
		
		html += " data-toggle='tooltip' ";
		
		return html;
	},
	
	/* 添加动作 */
	addEvent:function(){
		
		var btnId = this.element.attr("btn-id");
		
		this.elementID = btnId;
		
		window.page = this.component.page;
		
		Yacon.callBack(this.elementID+"_beforeClick", [this.component,this.component.page,this]);
		
		if(btnId == "add"){
			this.addClick();
		}
		
		else if(btnId == "edit"){
			this.editClick();
		}
		
		else if(btnId == "delete"){
			this.deleteClick();
		}
		
		else if(btnId == "save"){
			this.saveClick();
		}
		
		else if(btnId == "exit"){
			this.exitClick();
		}
		
		else if(btnId == "refresh"){
			this.refreshClick();
		}
		
		else if(btnId == "expand"){
			this.expandClick();
		}
		
		else if(btnId == "cancle"){
			this.cancleClick();
		}
		
		else {
			this.ownerClick();
		}
		
		Yacon.callBack(this.elementID+"_afterClick", [this.component,this.component.page,this]);
		
	},
	
	/* 添加动作 - 弹窗 */
	addClick: function(){
		
		var isAccess = true;
		
		if(Yacon.isfireuserEvent(this.component.elementID+"_beforeInsert")){
			isAccess = Yacon.fireUserEvent(this.component.elementID+"_beforeInsert", [this.component,this.component.page]);
		}
		
		if(isAccess){
			
			var detail = this.menu.parent.element.detail;
			
			var width = detail.width;
			var height = detail.height;
			var path =  detail.path;
			
			this.dataSet.setAction("type","1");
			
			/* 非弹窗情况 */
			if(path == undefined){
				
				this.component.createRow();		
				this.component.createRowEvent();
				
			}
			
			else{
				
				path = Yacon.addUrlParam("state", "insert", path);
				
				if(!Yacon.isEmpty(detail.param)){
					var params = detail.param;
					for(var i=0;i<params.length;i++){
						
						var paramsKey = params[i].key;
						var foreignKey = params[i].foreign;
						
						if(foreignKey.startsWith("#")){							
							foreignKey = foreignKey.substring(1);					
							path = Yacon.addUrlParam(paramsKey,this.component.page.element.parameter[foreignKey],path);							
						}
						else if(foreignKey.startsWith("$")){
							foreignKey = foreignKey.substring(1);	
							
							var master = this.component.getMaster();
							
							if(master){
								path = Yacon.addUrlParam(paramsKey,master.getFieldValue(foreignKey.toUpperCase()),path);
							}else{
								continue;
							}
							
						}	
						else{
							path = Yacon.addUrlParam(paramsKey,this.dataSet.getDataSetValue(foreignKey.toUpperCase()),path);							
						}
						
					}
				}
				
				//增加主表无记录不弹窗
				if(this.component.elementType == "tsqlgrid"){
					
					var masterTable = this.component.getMaster();
					
					if(masterTable){
						
						var len = $("div[lily-id='"+masterTable.elementID+"'][lily-type='"+masterTable.elementType+"'] .panel-body tbody tr").length;

						if(len == 0){
							return;
						}
						
					}
					
				}
				
				var autoClose = detail.autoClose;
				if(autoClose == undefined){
					autoClose = true;
				}
				
				/* 弹窗 */
				Yacon.showModal({
					heigth : height ,
					width : width ,
					url : path ,
					dataSet : this.dataSet,
					autoClose : autoClose
				});
				
			}
			
		}
		
		
	},
	
	/* 撤销操作 */
	cancleClick: function(){
		
		this.dataSet.refresh();
		
	},
	
	editClick: function(){
			
		var isAccess = true;
		
		if(Yacon.isfireuserEvent(this.component.elementID+"_beforeEdit")){
			isAccess = Yacon.fireUserEvent(this.component.elementID+"_beforeEdit", [this.component,this.component.page]);
		}
		
		if(isAccess){
			
			var detail = this.menu.parent.element.detail;
			
			var width = detail.width;
			var height = detail.height;
			var path =  detail.path;
			
			this.dataSet.setAction("type","2");
			
			if(this.component.elementType == "tsqlgrid"){
				
				var len = $("div[lily-id='"+this.component.elementID+"'][lily-type='"+this.component.elementType+"'] .panel-body tbody tr").length;
				
				if(len == 0){
					return;
				}
				
				this.dataSet.getDataSet("flat");
				
			}
			
			/* 非弹窗情况支持 */
			if(path == undefined){
					
				this.component.editRow();
				this.component.editRowEvent();
				
			}else{
				
				path = Yacon.addUrlParam("state", "edit", path);
				
				if(!Yacon.isEmpty(detail.param)){
					var params = detail.param;
					for(var i=0;i<params.length;i++){
						
						var paramsKey = params[i].key;
						var foreignKey = params[i].foreign;
						
						if(foreignKey.startsWith("#")){							
							foreignKey = foreignKey.substring(1);					
							path = Yacon.addUrlParam(paramsKey,this.component.page.element.parameter[foreignKey],path);							
						}
						else if(foreignKey.startsWith("$")){
							foreignKey = foreignKey.substring(1);	
							
							var master = this.component.getMaster();
							
							if(master){
								path = Yacon.addUrlParam(paramsKey,master.getFieldValue(foreignKey.toUpperCase()),path);
							}else{
								continue;
							}
							
						}	
						else{
							path = Yacon.addUrlParam(paramsKey,this.dataSet.getDataSetValue(foreignKey.toUpperCase()),path);							
						}
						
					}
				}
				
				/* 弹窗 */
				Yacon.showModal({
					heigth : height ,
					width : width ,
					url : path ,
					dataSet : this.dataSet
				});
				
			}
			
		}
		
	},
	
	deleteClick: function(){
		
		var isAccess = true;
		
		if(Yacon.isfireuserEvent(this.component.elementID+"_beforeDelete")){
			isAccess = Yacon.fireUserEvent(this.component.elementID+"_beforeDelete", [this.component,this.component.page]);
		}
		
		if(isAccess){
			
			this.dataSet.setAction("type","3");
			
			this.dataSet.getCheckDataSet();
			
			if(this.dataSet.getDataSetCount() <= 0){
				alert("至少选择一条记录！");
				return;
			}
			
			if(window.confirm("确定要进行删除操作吗？")){
				this.dataSet.switchMessage();
			}
			
			var result = this.dataSet.rsp.action.result;
			var msg = this.dataSet.rsp.action.message;
			var type = this.dataSet.rsp.action.type;
			
			this.dataSet.refresh();
			
		}
		
	},
	
	/* 保存  */
	saveClick: function(){
		
		/* 加载数据 */
		this.dataSet.instData();
			
		this.dataSet.switchMessage();
		
		var result = this.dataSet.rsp.action.result;
		var msg = this.dataSet.rsp.action.message;
		var type = this.dataSet.rsp.action.type;
		
		/* 成功 */
		if(result == "0000"){
			
			var isWindowControl = this.element.parent().attr("isWindowControl");
			
			if(!Yacon.isEmpty(isWindowControl) && Yacon.isTrue(isWindowControl)){
				if(this.page.windows){			
					this.page.windows.closeWindow();		
				}
			};
					
			/* 页面刷新 */
			if(this.dataSet.component.elementType == "tree"){}
			else{
				this.dataSet.refresh();
			}
					
		}
		
		
	},
	
	/* 退出 */
	exitClick: function(){	
		
		if(this.page.windows){			
			this.page.windows.closeWindow();		
		}	
	},
	
	/* 刷新 */
	refreshClick: function(){		
		this.dataSet.refresh();	
	},
	
	/* 收缩-伸展 */
	expandClick:function(){
		
		if(this.element.find("span").hasClass("glyphicon-chevron-up")){
			$("#"+this.grid.elementID+" .panel-body").slideDown();	
			$("#"+this.grid.elementID+" .panel-footer").slideDown();
			this.element.find("span").prop("class","glyphicon glyphicon-chevron-down icon");
		}
		else{
			$("#"+this.grid.elementID+" .panel-body").slideUp();	
			$("#"+this.grid.elementID+" .panel-footer").slideUp();
			this.element.find("span").prop("class","glyphicon glyphicon-chevron-up icon");
		}
		
	},
	
	/* 自定义方法 */
	ownerClick: function(){
		var functionName = this.element.attr("view-click");	
		
		if(this.component.elementType == "tsqlgrid"){
			this.component.getDataSet();
		}
			
		Yacon.callBack(functionName, [this.component,this.page,this]);
		
	},
	
	popover:function(content,title){
		
		if(Yacon.isDefined(content)){
			this.element.attr("data-content",content);
		}
		
		if(Yacon.isDefined(title)){
			this.element.attr("title",title);
		}
		
	},
	
	tooltip:function(content){
		
		if(Yacon.isDefined(content)){
			this.element.attr("title",content);
		}
		
	},
	
	popShow:function(){		
		this.element.popover('show');
	},
	
	popHide:function(){
		this.element.popover('hide');
	},
	
	popToggle:function(){		
		this.element.popover('toggle');
	},
	
	popDestroy:function(){
		this.element.popover('destroy');
	}
		
};

function addPopover(){
	
	var allBtn = $(".btn[data-toggle='popover']");
	
	for(var i=0;i<allBtn.length;i++){
		
		var btn = allBtn.eq(i);
		
		if(btn.attr("lily-load") == undefined){
			
			btn.popover({
		      	container: 'body'
			});

			btn.attr("lily-load","load");
			
		}	
		
	}
	
}

function addTooltip(){
	
	var allBtn = $(".btn[data-toggle='tooltip']");
	
	for(var i=0;i<allBtn.length;i++){
		
		var btn = allBtn.eq(i);
		
		if(btn.attr("lily-load") == undefined){
			
			btn.tooltip({
		      	container: 'body'
			});

			btn.attr("lily-load","load");
			
		}	
		
	}
	
}